package com.hctforgreen.greenservice;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.CallLog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.ResultEntity;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 提交电话反馈页面
 * 
 * @author Tallen Zhai
 * 
 */
public class SubmitPhoneFeedBackActivity extends ParentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_submit_phone_feed_back);

		initSkinLayout();

		initTitleButtonBar();
		initWindow();
	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.submit_phone_feed_back_title_hint));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initWindow() {
		final RadioGroup scoreRg = (RadioGroup) findViewById(R.id.rg_score);
		scoreRg.check(R.id.rb_type_1);
		final EditText content = (EditText) findViewById(R.id.et_content);
		final Button submit = (Button) findViewById(R.id.btn_submit);
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final String sScoreContent = content.getText().toString()
						.trim();
				if ("".equals(sScoreContent)) {
					Toast.makeText(SubmitPhoneFeedBackActivity.this,
							getString(R.string.add_feed_input_not_null_hint),
							Toast.LENGTH_SHORT).show();
					return;
				}
				String sScore = getScore(scoreRg);

				submit(v, sScore, sScoreContent, LoginResultStoreUtil
						.get(SubmitPhoneFeedBackActivity.this).personId,
						getTimeLength());

			}

			private String getTimeLength() {
				ContentResolver cr = getContentResolver();
				final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,
						new String[] { CallLog.Calls.NUMBER,
								CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE,
								CallLog.Calls.DATE, CallLog.Calls.DURATION },
						null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
				cursor.moveToPosition(0);
				long duration = cursor.getLong(4);
				return String.valueOf(duration);
			}

			private String getScore(final RadioGroup scoreRg) {
				String sScore = "1";
				if (scoreRg.getCheckedRadioButtonId() == R.id.rb_type_1) {
					sScore = "1";
				} else if (scoreRg.getCheckedRadioButtonId() == R.id.rb_type_2) {
					sScore = "2";
				} else if (scoreRg.getCheckedRadioButtonId() == R.id.rb_type_3) {
					sScore = "3";
				} else if (scoreRg.getCheckedRadioButtonId() == R.id.rb_type_4) {
					sScore = "4";
				} else if (scoreRg.getCheckedRadioButtonId() == R.id.rb_type_5) {
					sScore = "5";
				}
				return sScore;
			}
		});
	}

	private void submit(final View submit, final String score,
			final String scoreContent, final String personId,
			final String timeLength) {
		submit.setClickable(false);
		final ProgressBar loadPb = (ProgressBar) findViewById(R.id.pb_loading);
		loadPb.setVisibility(View.VISIBLE);
		MobclickAgent.onEvent(this, "Dial_feadback");
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				submit.setClickable(true);
				loadPb.setVisibility(View.GONE);
				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					hctResult = (HctResult) msg.obj;
					ResultEntity entity = (ResultEntity) hctResult.data;
					if (entity.statusCode
							.equals(HctConstants.RESULT_STATE_SUCCESS)) {
						Toast.makeText(SubmitPhoneFeedBackActivity.this,
								getString(R.string.add_feed_sucess_hint),
								Toast.LENGTH_SHORT).show();
						finish();
					} else {
						Toast.makeText(SubmitPhoneFeedBackActivity.this,
								getString(R.string.net_error_hint),
								Toast.LENGTH_SHORT).show();
					}
					break;
				case HctConstants.STATUS_FAILD:
					Toast.makeText(SubmitPhoneFeedBackActivity.this,
							getString(R.string.net_error_hint),
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(
							SubmitPhoneFeedBackActivity.this);

					HctResult result = controller.submitPhoneFeedBack(score,
							scoreContent, personId, timeLength);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}
}
